//
//  YCSessionViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/10.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  聊天/部落主界面

#import "YCSessionViewController.h"
#import "YCSessionRightViewButton.h"
#import "YCNavigationViewController.h"
#import "YCHomeViewController.h"
#import "YCMeHomeViewController.h"
#import "YCDropDownMenuView.h"
#import "YCAddFriendViewController.h"
#import "YCShopSearchViewController.h"
#import "YCScanFollowViewController.h"
#import "YCMyQRCodeViewController.h"

#import "YCAddressBookViewController.h"//通讯录
#import "YCEditDailyRecodeViewController.h"//发日志
#import "YCEditPhotographyViewController.h"//发影集

#pragma mark - YCSessionCell

@interface YCSessionCell : UITableViewCell

@property (nonatomic,readonly) UIImageView *sessionImageView;
@property (nonatomic,readonly) UILabel *sessionTitleLabel;
@property (nonatomic,readonly) UILabel *sessionDetailLabel;
@property (nonatomic,readonly) JSBadgeView *badgeView;

@end

@implementation YCSessionCell

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _sessionImageView = [[UIImageView alloc] init];
        _sessionImageView.layer.cornerRadius = 44.0 / 2.0;
        _sessionImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.sessionImageView];
        
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_sessionImageView);
            make.size.equalTo(_sessionImageView);
        }];
        
        [self.sessionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.and.height.equalTo(@44.0);
            make.left.equalTo(@20);
            make.centerY.equalTo(self.contentView);
        }];
        
        _sessionTitleLabel = [[UILabel alloc] init];
        _sessionDetailLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.sessionTitleLabel];
        
        [self.sessionTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.sessionImageView.mas_right).offset(14);
            make.top.equalTo(self.sessionImageView);
        }];
        
        _sessionDetailLabel = [[UILabel alloc] init];
        _sessionDetailLabel.font = [UIFont fontWithName:@"DFHanziPenW3-GB" size:16];
        _sessionDetailLabel.textColor = [UIColor yc_colorWithUInt:0xb167a5];
        [self.contentView addSubview:self.sessionDetailLabel];
        
        [self.sessionDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.sessionImageView.mas_right).offset(14);
            make.right.equalTo(self.contentView.mas_right).offset(-20);
            make.top.equalTo(self.sessionTitleLabel.mas_bottom).offset(6);
        }];
        
        _badgeView = [[JSBadgeView alloc] initWithParentView:view alignment:JSBadgeViewAlignmentTopRight];
    }
    return self;
}

@end


#pragma mark -  YCSessionViewController

@interface YCSessionViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 表  */
@property (nonatomic, strong) UITableView *tableView;
/** 顶部segment */
@property (nonatomic, strong) UIView *topView;
/** 右边条  */
@property (nonatomic, strong) UIView *rightView;
/** 底部条  */
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) JSBadgeView *badgeView;
@property (nonatomic, strong) UIImageView *tipImageView;//无消息时提示:没有地球人发消息给你

/** 聊天  */
@property (nonatomic, strong) YCSessionRightViewButton *chatBtn;
/** 部落  */
@property (nonatomic, strong) YCSessionRightViewButton *tribeBtn;

/** 通讯录  */
@property (nonatomic, strong) YCSessionRightViewButton *addressBookBtn;
/** 发日志  */
@property (nonatomic, strong) YCSessionRightViewButton *sendDailyRecordButton;

/** segment  */
@property (nonatomic, strong) UISegmentedControl *segmentedControl;//部落(所有部落,我围观的)

@property (nonatomic, strong) NSMutableArray *sessions;//对话数据

@end

@implementation YCSessionViewController

#pragma mark - 懒加载

- (NSMutableArray *)sessions
{
    if(!_sessions)
    {
        _sessions = [[NSMutableArray alloc] init];
    }
    return _sessions;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        CGRect rect = CGRectMake(0, 0, self.view.yc_width - 50, self.view.yc_height - 64-50);
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.rowHeight = 75;//
    }
    return _tableView;
}
// 聊天
- (YCSessionRightViewButton *)chatBtn
{
    if (!_chatBtn) {
        YCSessionRightViewButton *chatBtn = [[YCSessionRightViewButton alloc] init];
        [chatBtn setImage:[UIImage yc_roundPlaceHolderImageForSize:CGSizeMake(30, 30)] forState:UIControlStateNormal];
        [chatBtn setImage:[UIImage yc_roundPlaceHolderImageForSize:CGSizeMake(40, 40)] forState:UIControlStateSelected];
        [chatBtn setTitle:@"聊天" forState:UIControlStateNormal];
        [chatBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        chatBtn.imageView.contentMode = UIViewContentModeCenter;
        //默认为聊天界面
        _chatBtn = chatBtn;
        chatBtn.selected = YES;
    }
    return _chatBtn;
}

// 部落
- (YCSessionRightViewButton *)tribeBtn
{
    if (!_tribeBtn) {
        
        YCSessionRightViewButton *tribeBtn = [[YCSessionRightViewButton alloc] init];
        [tribeBtn setImage:[UIImage imageNamed:@"部落-未选择"] forState:UIControlStateNormal];
        [tribeBtn setImage:[UIImage imageNamed:@"部落-选择"] forState:UIControlStateSelected];
        [tribeBtn setTitle:@"部落" forState:UIControlStateNormal];
        [tribeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        tribeBtn.imageView.contentMode = UIViewContentModeCenter;
        _tribeBtn = tribeBtn;
        tribeBtn.selected = NO;
    }
    return _tribeBtn;
}
// 通讯录
- (YCSessionRightViewButton *)addressBookBtn
{
    if (!_addressBookBtn) {
        // 聊天界面中显示
        YCSessionRightViewButton *addressBookBtn = [[YCSessionRightViewButton alloc] init];
        [addressBookBtn setImage:[UIImage imageNamed:@"通讯录"] forState:UIControlStateNormal];
        [addressBookBtn setTitle:@"通讯录" forState:UIControlStateNormal];
        addressBookBtn.imageView.contentMode = UIViewContentModeCenter;
        _addressBookBtn = addressBookBtn;
        _addressBookBtn.hidden = NO;
    }
    return _addressBookBtn;
}
// 发日志
- (YCSessionRightViewButton *)sendDailyRecordButton
{
    if (!_sendDailyRecordButton) {
        // 部落界面才显示
        YCSessionRightViewButton *sendDailyRecordButton = [[YCSessionRightViewButton alloc] init];
        [sendDailyRecordButton setImage:[UIImage imageNamed:@"发日志"] forState:UIControlStateNormal];
        [sendDailyRecordButton setTitle:@"发日志" forState:UIControlStateNormal];
        sendDailyRecordButton.imageView.contentMode = UIViewContentModeCenter;
        _sendDailyRecordButton = sendDailyRecordButton;
        sendDailyRecordButton.hidden = YES;
    }
    return _sendDailyRecordButton;
}

// 顶部segment的容器
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        [_topView addSubview:self.segmentedControl];
        [self.segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_topView);
            make.centerX.equalTo(_topView);
            make.left.equalTo(_topView).offset(20);
            make.right.equalTo(_topView).offset(-20);
            make.height.equalTo(@25);
        }];
    }
    return _topView;
}

// 聊天 部落
- (UIView *)rightView
{
    if (!_rightView) {
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = [UIColor whiteColor];
        
        [_rightView addSubview:self.chatBtn];
        [_rightView addSubview:self.tribeBtn];
        [_rightView addSubview:self.addressBookBtn];
        [_rightView addSubview:self.sendDailyRecordButton];
        
        [self.chatBtn addTarget:self action:@selector(didTouchUpInsideChatButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.tribeBtn addTarget:self action:@selector(didTouchUpInsideTribeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.addressBookBtn addTarget:self action:@selector(didTouchUpInsideAddressBookButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.sendDailyRecordButton addTarget:self action:@selector(didTouchUpInsideSendDailyRecordButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_rightView).offset(20);
            make.centerX.equalTo(_rightView);
            make.width.equalTo(@40);
            make.height.equalTo(@60);
        }];
        
        [self.tribeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.chatBtn.mas_bottom).offset(30);
            make.centerX.equalTo(_rightView);
            make.width.equalTo(@40);
            make.height.equalTo(@60);
        }];
        
        [self.addressBookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_rightView).offset(-20);
            make.centerX.equalTo(_rightView);
            make.width.equalTo(@40);
            make.height.equalTo(@60);
        }];
        
        [self.sendDailyRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.left.equalTo(self.addressBookBtn);
        }];
        
    }
    return _rightView;
}

// 底部条
- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        
        UIButton *button1 = [[UIButton alloc] init];
        [button1 setImage:[UIImage imageNamed:@"一乘未选中"] forState:UIControlStateNormal];
        [button1 setImage:[UIImage imageNamed:@"一乘"] forState:UIControlStateSelected];
        button1.imageView.contentMode = UIViewContentModeCenter;
        
        UIButton *button2 = [[UIButton alloc] init];
        [button2 setImage:[UIImage imageNamed:@"聊天"] forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"聊天选中"] forState:UIControlStateSelected];
        button2.selected = YES;
        button2.imageView.contentMode = UIViewContentModeCenter;
        
        UIButton *button3 = [[UIButton alloc] init];
        [button3 setImage:[UIImage imageNamed:@"我"] forState:UIControlStateNormal];
        button3.imageView.contentMode = UIViewContentModeCenter;
        
        // 分隔线
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
        
        [_bottomView addSubview:button1];
        [_bottomView addSubview:button2];
        [_bottomView addSubview:button3];
        [_bottomView addSubview:line];
        
        [button1 addTarget:self action:@selector(didTouchUpInsideLeftButton:) forControlEvents:UIControlEventTouchUpInside];
        [button2 addTarget:self action:@selector(didTouchUpInsideMiddleButton:) forControlEvents:UIControlEventTouchUpInside];
        [button3 addTarget:self action:@selector(didTouchUpInsideRightButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bottomView.mas_left).offset(20);
            make.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.and.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bottomView.mas_right).offset(-20);
            make.centerY.equalTo(_bottomView);
            make.width.and.height.equalTo(@50);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(_bottomView);
            make.height.equalTo(@1);
        }];
    }
    return _bottomView;
}

- (UIImageView *)tipImageView
{
    if (!_tipImageView)
    {
        _tipImageView = [[UIImageView alloc] init];
        [_tipImageView setImage:[UIImage imageNamed:@"没有地球人向你发消息"]];//没有地球人发消息给你
        _tipImageView.contentMode = UIViewContentModeCenter;
        _tipImageView.hidden = YES;
    }
    
    return _tipImageView;
}

- (UISegmentedControl *)segmentedControl
{
    if (!_segmentedControl)
    {
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"所有部落",@"我围观的"]];
        _segmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
        _segmentedControl.tintColor = [UIColor blackColor];
        _segmentedControl.selectedSegmentIndex = 0;
        [_segmentedControl addTarget:self action:@selector(didChangeValueOfSegmentedControl:) forControlEvents:UIControlEventValueChanged];
//        _segmentedControl.hidden = NO;
    }
    
    return _segmentedControl;
}

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"联系";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"加号" highlightImage:@"加号" target:self action:@selector(displayDropView:)];
    [self makeView];
    
    if (self.sessions.count == 0)
    {
        self.tipImageView.hidden = NO;
//        self.tableView.scrollEnabled = NO;
    }
    else
    {
        self.tipImageView.hidden = YES;
//        self.tableView.scrollEnabled = YES;
        [self.tableView reloadData];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)makeView
{
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.rightView];
    [self.view addSubview:self.bottomView];
    [self.tableView addSubview:self.tipImageView];
    
    [self.tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.tableView);
    }];
//    self.tableView.backgroundColor = [UIColor redColor];
//    self.rightView.backgroundColor = [UIColor greenColor];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
        make.width.equalTo(@50);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.view);
        make.right.equalTo(self.rightView.mas_left);
        make.height.equalTo(@0);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.topView.mas_bottom);
        make.right.equalTo(self.rightView.mas_left);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}



#pragma mark - UITableView dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;//self.sessions.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //   contactChat *chat = self.sessions[indexPath.row];
    YCSessionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
    if(!cell)
    {
        cell = [[YCSessionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sessionCell"];
    }
    cell.sessionTitleLabel.text = @"阳教练";
    [cell.sessionImageView yc_setWebImageWithURLString:@"" holderSize:CGSizeMake(37.0, 37.0)];
    cell.sessionDetailLabel.text = @"下午3点记得考试";
//    if(chat.unReadBadge > 0)
//    {
//        cell.badgeView.badgeText = [NSString stringWithFormat:@"%ld",(long)[chat unReadBadge]];
//    }
//    else
//    {
//        cell.badgeView.badgeText = nil;
//    }
    return cell;
}

#pragma mark - UITableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 监听点击事件

// 下拉弹出菜单
- (void)displayDropView:(UIButton *)button
{
    YCDropDownMenuView *dropView = [[YCDropDownMenuView alloc] initWithFrame:CGRectMake(kScreenWidth - 130 , 50, 125, 185) itemDictArray:@[@{@"icon":@"添加好友",@"title":@"添加好友"},@{@"icon":@"搜商家",@"title":@"搜商家"},@{@"icon":@"扫一扫",@"title":@"扫一扫"},@{@"icon":@"我的二维码",@"title":@"我的二维码"}]];
    [dropView showWithDidSelectRowOfIndexPathBlock:^(NSIndexPath *indexPath) {
        YCLog(@"点击了第%zd行",indexPath.row);
        switch (indexPath.row) {
            case 0://添加好友
            {
                YCAddFriendViewController *addfriendVc = [[YCAddFriendViewController alloc] init];
                [self.navigationController pushViewController:addfriendVc animated:YES];
            }
            break;
                
            case 1://搜商家
            {
                YCShopSearchViewController *shopSearchVc = [[YCShopSearchViewController alloc] init];
                [self.navigationController pushViewController:shopSearchVc animated:YES];
            }
            break;
                
            case 2://扫一扫
            {
                YCScanFollowViewController *addfriendVc = [[YCScanFollowViewController alloc] init];
                [self.navigationController pushViewController:addfriendVc animated:YES];
            }
            break;
              
            case 3://我的二维码
            {
                YCMyQRCodeViewController *myQRCodeVc = [[YCMyQRCodeViewController alloc] init];
                [self.navigationController pushViewController:myQRCodeVc animated:YES];
            }
            break;
            default:
                break;
        }
    }];
}

/***** 上边segment *****/
- (void)didChangeValueOfSegmentedControl:(UISegmentedControl *)segment
{
    YCLog(@"SegmentedControl:前当选择段:%zd",segment.selectedSegmentIndex);
    
    if (segment.selectedSegmentIndex == 0)
    {
        // 所有部落
    }
    else
    {
        // 我围观的
        
    }
}


/******* 右边条 *******/
// 聊天
- (void)didTouchUpInsideChatButton:(UIButton *)button
{
    YCLogFunc
    self.chatBtn.selected = YES;
    self.tribeBtn.selected = NO;
    //隐藏或显示
    self.addressBookBtn.hidden = NO;
    self.sendDailyRecordButton.hidden = YES;
    
    // 修改约束
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0);
    }];
}

// 部落
- (void)didTouchUpInsideTribeButton:(UIButton *)button
{
    YCLogFunc
    self.chatBtn.selected = NO;
    self.tribeBtn.selected = YES;
    //隐藏或显示
    self.addressBookBtn.hidden = YES;
    self.sendDailyRecordButton.hidden = NO;
    
    // 修改约束
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@40);
    }];
}

// 通讯录
- (void)didTouchUpInsideAddressBookButton:(UIButton *)button
{
    YCLogFunc
    YCAddressBookViewController *addressBookVc = [[YCAddressBookViewController alloc] init];
    [self.navigationController pushViewController:addressBookVc animated:YES];
}
// 发日志
- (void)didTouchUpInsideSendDailyRecordButton:(UIButton *)button
{
    YCLogFunc
    // 暂时用actionsheet
    LXActionSheet *sheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:@[@"日志",@"影集"]];
    [sheet showInView:self.view mask:YES maskColor:[UIColor yc_colorWithUInt:0x808080] click:^(NSUInteger index, LXActionSheet *sheet) {
        switch (index) {
            case 1://日志
            {
                YCEditDailyRecodeViewController *dailyVc = [[YCEditDailyRecodeViewController alloc] init];
                [self.navigationController pushViewController:dailyVc animated:YES];
            }
            break;
            case 2://影集
            {
                YCEditPhotographyViewController *photographyVc = [[YCEditPhotographyViewController alloc] init];
                [self.navigationController pushViewController:photographyVc animated:YES];
            }
            break;
            default:
                break;
        }
        
    } cancel:nil];
}



/*******底部条*******/
- (void)didTouchUpInsideLeftButton:(UIButton *)button
{
    YCLogFunc
    YCHomeViewController *rootVc = [[YCHomeViewController alloc] init];
    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}

// 聊天
- (void)didTouchUpInsideMiddleButton:(UIButton *)button
{
    YCLogFunc
    
}

//个人页
- (void)didTouchUpInsideRightButton:(UIButton *)button
{
    UIViewController *rootVc = [[YCMeHomeViewController alloc] init];
    YCNavigationViewController *nav = [[YCNavigationViewController alloc] initWithRootViewController:rootVc];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}



@end
